/*
 * Copyright (C) 2016 alchemystar, Inc. All Rights Reserved.
 */
package com.lazyboyl.spring.boot.sharding.sphere.proxy.net.handler.fronted;


import com.lazyboyl.spring.boot.sharding.sphere.proxy.net.proto.util.ErrorCode;
import com.lazyboyl.spring.boot.sharding.sphere.proxy.parse.ServerParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ServerQueryHandler
 *
 * @Author lizhuyang
 */
public class ServerQueryHandler implements FrontendQueryHandler {

    private static final Logger logger = LoggerFactory.getLogger(ServerQueryHandler.class);

    private FrontendConnection source;

    public ServerQueryHandler(FrontendConnection source) {
        this.source = source;
    }

    @Override
    public void query(String sql) {

//        logger.info("sql = " + sql);

        int rs = ServerParse.parse(sql);
        switch (rs & 0xff) {
            case ServerParse.DELETE:
                logger.info("DELETE sql => {} ", sql);
                source.execute(sql, rs);
                break;
            case ServerParse.INSERT:
                logger.info("INSERT sql => {} ", sql);
                source.execute(sql, rs);
                break;
            case ServerParse.UPDATE:
                logger.info("UPDATE sql => {} ", sql);
                source.execute(sql, rs);
                break;
            case ServerParse.EXPLAIN:
                // ExplainHandler.handle(sql, c, rs >>> 8);
                break;
            case ServerParse.SET:
                SetHandler.handle(sql, source, rs >>> 8);
                break;
            case ServerParse.SHOW:
                // todo data source
                ShowHandler.handle(sql, source, rs >>> 8);
                break;
            case ServerParse.SELECT:
                logger.info("SelectHandler sql => {} ", sql);
                SelectHandler.handle(sql, source, rs >>> 8);
                break;
            case ServerParse.START:
                StartHandler.handle(sql, source, rs >>> 8);
                break;
            case ServerParse.BEGIN:
                BeginHandler.handle(sql, source);
                break;
            case ServerParse.SAVEPOINT:
                SavepointHandler.handle(sql, source);
                break;
            case ServerParse.KILL:
                KillHandler.handle(sql, rs >>> 8, source);
                break;
            case ServerParse.KILL_QUERY:
                source.writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Unsupported command");
                break;
            case ServerParse.USE:
                UseHandler.handle(sql, source, rs >>> 8);
                break;
            case ServerParse.COMMIT:
                source.commit();
                break;
            case ServerParse.ROLLBACK:
                source.rollback();
                break;
            default:
                source.execute(sql, rs);
        }
    }
}
